<?php
/* ===========================
 * * 栏目类
 * @ by 子不语 爱不语CMS
 * @ QQ:76992327
 * @ Create:2014.04.6
 * @ Modify:2014.10.10
  ============================ */
if (!defined('KKINC')) exit('Request Error!');
class arctype extends parse {
    function str_array($str) {
        $arr = parent::str_array($str);
        $arr['where'].=' and isshow=1';
        if ($arr['tid']) {
            if ($arr['type'] == 'sun') {
                $arr['where'] .= ' and `fid`=' . $arr['tid'];
            } elseif ($arr['type'] == 'self') {
                $arr_arctype = $this->get_arctype($arr['tid']);
                $arr['where'] .= ' and `fid`=' . $arr_arctype['fid'];
            } else {
                $arr['where'] .= ' and `fid`=' . $arr['tid'];
            }
        } else if ($arr['in']) {
            $sonid = $this->get_arctype_son_sql($arr['in']);
            $arr['where'] .= ' and fid  in (' . $sonid . ')';
        } else {
            $arr['where'].=' and `fid`=0';
        }
        if ($arr['notin']) {
            $sonid = $this->get_arctype_son_sql($arr['in']);
            $arr['where'] .= ' and fid not in (' . $sonid . ')';
        }
        if ($arr['short']) {
            $arr['where'].=' and `shortname`=\'' . $arr['short'] . '\'';
        }
        if ($arr['jing']) {
            $arr['where'].=' and `singlepage`=\'' . $arr['jing'] . '\'';
        }
        if (!$arr['orderby']) {
            $arr['orderby'] = 'sortrank asc,id asc';
        }
        return $arr;
    }

    function get_list($str) {
        global $page;
        $arr = $this->str_array($str);
        $sql = 'select id,fid,topid,typename,shortname,jumpurl,litpic,typedir' . $arr['field'] . ' from `__arctype` ' . $arr['where'] . ' order by ' . $arr['orderby'] . $arr['num'];
        $query = db::_query($sql);
        while ($row = db::_object($query)) {
            if ($row->con) $row->con = stripslashes($row->con);
            $row->typeurl = U('list',array('tid'=>$row->id,'page'=> $page));
            $row->litpic = $this->replace_pic($row->litpic);
            $result[] = $row;
        }
        return $result;
    }

    /* ===============
     * *获取当前栏目信息
     *   @param $id int  栏目tid
     *   return array
      ========================= */
    function get_one($tid) {
        $res = db::_query('select * from `__arctype` where `id`=' . $tid . ' limit 1');
        $result = db::_object($res);
        $result->con = stripslashes($result->con);
        $result->typeurl = U('list',array('tid'=>$result->id));
        $result->litpic = $this->replace_pic($result->litpic);
        $result->pic = str_replace('sk/', '', $result->litpic);
        return $result;
    }
    function get_location($tid){
        global $k_webname,$k_weburl;
        $ac=$this->get_arctype();
        $string='<a href="'.$k_weburl.'">'.$k_webname.'</a> > ';
        if($ac[$tid])$string.='<a href="'.U('list',array('tid'=>$tid)).'">'.$ac[$tid]['typename'].'</a> > ';
        $parentid=$ac[$parentid]['fid'];
        while(true){
            if($ac[$parentid]){
                $string.='<a href="'.U('list',array('tid'=>$parentid)).'">'.$ac[$parentid]['typename'].'</a> > ';
                $parentid=$ac[$parentid]['fid'];
            }else{
                break;
            }
        }
        return $string;
    }
    /* ===================
     *  获取选项列表
     *
     * @access    public
     * @param     string  $selid  选择ID
     * @param     string  $userCatalog  用户类目
     * @param     string  $channeltype  频道类型
     * @return    string
      =================== */
    function GetOptionList($selid = 0) {
        //当前选中的栏目
        if ($selid > 0) {
            $row = db::get_one("SELECT id,typename FROM `__arctype` WHERE id='$selid'");
            $OptionArrayList .= "<option value='" . $row->id . "' selected='selected'>" . $row->typename . "</option>\r\n";
        }
        $sql = "select id,typename from `__arctype` where  fid=0 order by id asc";
        $res = db::_query($sql);
        while ($row = db::_object($res)) {
            $sonCats = '';
            $js = 1;
            $this->LogicGetOptionArray($row->id, '&nbsp;─', $sonCats, $js);
            if ($sonCats != '') {
                $OptionArrayList .= "<option value='" . $row->id . "' class='option'>" . $row->typename . "</option>\r\n";
                $OptionArrayList .= $sonCats;
            } else {
                $OptionArrayList .= "<option value='" . $row->id . "' class='option'>" . $row->typename . "</option>\r\n";
            }
        }
        return $OptionArrayList;
    }

    function LogicGetOptionArray($id, $step, &$sonCats, $js) {
        $res = db::_query("Select id,typename From `__arctype` where fid='" . $id . "' And singlepage=0 order by id asc");
        while ($row = db::_object($res)) {
            $sonCats .= "<option value='" . $row->id . "' class='option" . $js . "'>$step" . $row->typename . "</option>\r\n";
            $js++;
            $this->LogicGetOptionArray($row->id, $step . '─', $sonCats, $js);
            $js--;
        }
    }


}